package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.WaterMeterReadingServiceOtherProdsAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author liaojing
 * NB物联网水务抄表服务-超声波--子产品和附加商品的型号属性联动
 * @create 2022-01-19-11:08
 */
@Service
@AttrHandler(WaterMeterReadingServiceOtherProdsAttrHandler.class)
public class WaterMeterReadingServiceOtherProdsLinkAttrHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String attrValue = attr.getValue();
        String custId = data.getCustId();
        String prodInstId = inst.getInstId();
        String sceneInstId = data.getSceneInstId();

        //公允价属性处理 (获取公允价属性)
        KiteAttr fairPriceAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004115");

        //物资编码属性处理
        KiteAttr materialIdAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004178");

        //水表框架
        //型号对应的物资编码
        if(KiteStringUtils.isEqual(inst.getSpecId(),"63249657")){
            if(Objects.nonNull(materialIdAttr)){
                //NB物联网水务抄表服务-超声波水表DN15-汇中外壳 - 7600301118
                setHandleAttrValue ("110010396", attrValue, inst, "200004178", "7600301118", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-汇中外壳 - 7600301119
                setHandleAttrValue ("110010397", attrValue, inst, "200004178", "7600301119", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-汇中外壳 - 7600301120
                setHandleAttrValue ("110010398", attrValue, inst, "200004178", "7600301120", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN15-阀控-汇中外壳 - 7600301352
                setHandleAttrValue ("110010399", attrValue, inst, "200004178", "7600301352", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-阀控-汇中外壳 - 7600301353
                setHandleAttrValue ("110010400", attrValue, inst, "200004178", "7600301353", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-阀控-汇中外壳 - 7600301354
                setHandleAttrValue ("110010401", attrValue, inst, "200004178", "7600301354", sceneInstId);
            }
        }

        //外壳等塑料件
        //型号对应的公允价
        if(KiteStringUtils.isEqual(inst.getSpecId(),"63249708")){
            if(Objects.nonNull(fairPriceAttr)){
                //NB物联网水务抄表服务-超声波水表DN15-汇中外壳-25元
                setHandleAttrValue ("110010403", attrValue, inst, "200004115", "25", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-汇中外壳-26元
                setHandleAttrValue ("110010404", attrValue, inst, "200004115", "26", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-汇中外壳-27元
                setHandleAttrValue ("110010405", attrValue, inst, "200004115", "27", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN15-阀控-汇中外壳-30元
                setHandleAttrValue ("110010406", attrValue, inst, "200004115", "30", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-阀控-汇中外壳-31元
                setHandleAttrValue ("110010407", attrValue, inst, "200004115", "31", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-阀控-汇中外壳-33元
                setHandleAttrValue ("110010408", attrValue, inst, "200004115", "33", sceneInstId);
            }
        //型号对应的物资编码
            if(Objects.nonNull(materialIdAttr)){
                //NB物联网水务抄表服务-超声波水表DN15-汇中外壳 - 7600301118-1
                setHandleAttrValue ("110010403", attrValue, inst, "200004178", "7600301118-1", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-汇中外壳 - 7600301119-1
                setHandleAttrValue ("110010404", attrValue, inst, "200004178", "7600301119-1", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-汇中外壳 - 7600301120-1
                setHandleAttrValue ("110010405", attrValue, inst, "200004178", "7600301120-1", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN15-阀控-汇中外壳 - 7600301352-1
                setHandleAttrValue ("110010406", attrValue, inst, "200004178", "7600301352-1", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-阀控-汇中外壳 - 7600301353-1
                setHandleAttrValue ("110010407", attrValue, inst, "200004178", "7600301353-1", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-阀控-汇中外壳 - 7600301354-1
                setHandleAttrValue ("110010408", attrValue, inst, "200004178", "7600301354-1", sceneInstId);
            }
        }

        //超声波水表主控板
        //型号对应的公允价
        if(KiteStringUtils.isEqual(inst.getSpecId(),"63249660")){
            if(Objects.nonNull(fairPriceAttr)){
                //NB物联网水务抄表服务-超声波水表DN15-汇中外壳-277
                setHandleAttrValue ("110010409", attrValue, inst, "200004115", "277", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-汇中外壳-287
                setHandleAttrValue ("110010410", attrValue, inst, "200004115", "287", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-汇中外壳-298
                setHandleAttrValue ("110010411", attrValue, inst, "200004115", "298", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN15-阀控-汇中外壳-330
                setHandleAttrValue ("110010412", attrValue, inst, "200004115", "330", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-阀控-汇中外壳-339
                setHandleAttrValue ("110010413", attrValue, inst, "200004115", "339", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-阀控-汇中外壳-360
                setHandleAttrValue ("110010414", attrValue, inst, "200004115", "360", sceneInstId);
            }
            //型号对应的物资编码
            if(Objects.nonNull(materialIdAttr)){
                //NB物联网水务抄表服务-超声波水表DN15-汇中外壳 - 7600301118
                setHandleAttrValue ("110010409", attrValue, inst, "200004178", "7600301118", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-汇中外壳 - 7600301119
                setHandleAttrValue ("110010410", attrValue, inst, "200004178", "7600301119", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-汇中外壳 - 7600301120
                setHandleAttrValue ("110010411", attrValue, inst, "200004178", "7600301120", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN15-阀控-汇中外壳 - 7600301352
                setHandleAttrValue ("110010412", attrValue, inst, "200004178", "7600301352", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN20-阀控-汇中外壳 - 7600301353
                setHandleAttrValue ("110010413", attrValue, inst, "200004178", "7600301353", sceneInstId);
                //NB物联网水务抄表服务-超声波水表DN25-阀控-汇中外壳 - 7600301354
                setHandleAttrValue ("110010414", attrValue, inst, "200004178", "7600301354", sceneInstId);
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
    private void setHandleAttrValue (String key, String oldValue, KiteInst inst, String newFieldName,
                                     String newValue, String sceneInstId ) {
        if (KiteStringUtils.isEqual(key, oldValue)) {
            operateInstAttrService.setAttrValue(inst, newFieldName, newValue, sceneInstId, null);
        }
    }
}
